package com.jxcq.tensquare.base.controller;

import com.jxcq.tensquare.base.pojo.Label;
import com.jxcq.tensquare.base.service.LabelService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author CalvinXCui
 * @date 2019- 10- 12 17:00
 */
@RestController
@CrossOrigin
@RequestMapping("label")
public class LabelController {

    @Autowired
    private LabelService labelService;

    @GetMapping
    public Result findAll(){
        return new Result(true, StatusCode.SUCCESS ,"查询成功",labelService.findAll());

    }

    @GetMapping("{labelId}")
    public Result findById(@PathVariable("labelId") String id){
        return new Result(true, StatusCode.SUCCESS ,"查询成功",labelService.findById(id));
    }

    @PostMapping
    public Result save(@RequestBody Label label){
        labelService.save(label);
        return new Result(true, StatusCode.SUCCESS ,"添加成功");
    }

    @PutMapping("{labelId}")
    public Result update(@PathVariable String labelId , @RequestBody Label label){
        label.setId(labelId);
        labelService.update(label);
        return new Result(true, StatusCode.SUCCESS ,"更新成功");
    }

    @DeleteMapping("{labelId}")
    public Result deleteById(@PathVariable String labelId){
        labelService.deleteById(labelId);
        return new Result(true, StatusCode.SUCCESS ,"删除成功");
    }

    @PostMapping("serech")
    public Result findSerech(@RequestBody Label label){
        List<Label> list = labelService.findSerech(label);
        return new Result(true, StatusCode.SUCCESS ,"查询成功",list);
    }

    @PostMapping("serech/{page}/{size}")
    public Result findPageSerech(@RequestBody Label label,@PathVariable int page,@PathVariable int size){
        Page<Label> pageData = labelService.findPageSerech(label,page,size);
        return new Result(true, StatusCode.SUCCESS ,"查询成功",new PageResult<Label>(pageData.getTotalElements(),pageData.getContent()));
    }
}
